import React, { useState, useEffect, useRef, useImperativeHandle } from 'react';
import { connect } from 'dva';
import { Row, Col, Form, Input, message, Spin, Radio, Select } from 'antd';
import request from '@/utils/request';

const { TextArea } = Input;

const formItemLayoutText = {
  labelCol: {
    xs: { span: 24 },
    sm: { span: 4 },
  },
  wrapperCol: {
    xs: { span: 24 },
    sm: { span: 20 },
  },
};

const isEmptyObject = obj => {
  if (obj === undefined || Object.keys(obj).length === 0) {
    return true;
  }
  return false;
};

function GuideInfoAdd(props) {
  const { getFieldDecorator } = props.form;
  const [spinning, setSpinning] = useState(false);
  const [guideTypeMap, setGuideType] = useState([]);
  const [orderFromMap, setOrderFrom] = useState([]);

  useEffect(() => {
    request('pmc/pms/StaticDataController/getStaticAttr.do', {
      data: {
        busiObjNbr: 'OrderExceptionGuideInfo',
        attrNbr: 'guideType',
      },
    }).then(res => {
      setGuideType(res);
    });
    request('pmc/pms/StaticDataController/getStaticAttr.do', {
      data: {
        busiObjNbr: 'OrderExceptionModel',
        attrNbr: 'orderFrom',
      },
    }).then(res => {
      setOrderFrom(res);
    });
  }, []);

  // 新增
  useImperativeHandle(props.cRef, () => ({
    handleAdd: () => {
      return new Promise(function(resolve) {
        props.form.validateFields((err, value) => {
          if (err) return;

          setSpinning(true);

          if (props.attrSpecAddType === 'add') {
            request('pmc/pms/attrSpec/add', { data: value }).then(res => {
              if (res && res.success === true) {
                setSpinning(false);
                message.success('新增成功');
                resolve(true);
              } else {
                setSpinning(false);
                if (res && res.errMessage) {
                  message.error(res.errMessage);
                } else {
                  message.error('新增失败');
                }
              }
            });
          } else if (props.attrSpecAddType === 'modify') {
            request('pmc/pms/attrSpec/update', { data: value }).then(res => {
              if (res && res.success === true) {
                setSpinning(false);
                message.success('修改成功');
                resolve(true);
              } else {
                setSpinning(false);
                if (res && res.errMessage) {
                  message.error(res.errMessage);
                } else {
                  message.error('修改失败');
                }
              }
            });
          }
        });
      });
    },
  }));

  return (
    <Spin spinning={spinning}>
      <Form>
        <Row>
          <Col span={24}>
            <Form.Item label="指引类型" {...formItemLayoutText}>
              {getFieldDecorator('guideType', {
                rules: [
                  {
                    required: true,
                    message: '指引类型不能为空',
                  },
                ],
              })(
                <Radio.Group>
                  {guideTypeMap.map(item => {
                    return <Radio value={item.attrValue}>{item.attrValueName}</Radio>;
                  })}
                </Radio.Group>
              )}
            </Form.Item>
          </Col>
        </Row>

        <Row>
          <Col span={24}>
            <Form.Item label="指引名称" {...formItemLayoutText}>
              {getFieldDecorator('guideName', {
                rules: [
                  {
                    required: true,
                    message: '指引名称不能为空',
                  },
                ],
              })(<Input allowClear placeholder="请输入" maxLength={50} />)}
            </Form.Item>
          </Col>
        </Row>
        <Row>
          <Col span={24}>
            <Form.Item label="系统来源" {...formItemLayoutText}>
              {getFieldDecorator('orderFrom', {
                rules: [
                  {
                    required: true,
                    message: '系统来源不能为空',
                  },
                ],
              })(
                <Select allowClear placeholder="请选择">
                  {orderFromMap.map(item => {
                    return (
                      <Select.Option value={item.attrValue}>{item.attrValueName}</Select.Option>
                    );
                  })}
                </Select>
              )}
            </Form.Item>
          </Col>
        </Row>
        <Row>
          <Col span={24}>
            <Form.Item label="提示内容" {...formItemLayoutText}>
              {getFieldDecorator('tipsContent', {
                rules: [
                  {
                    required: true,
                    message: '提示内容不能为空',
                  },
                ],
              })(<TextArea rows={5} placeholder="请输入" />)}
            </Form.Item>
          </Col>
        </Row>
      </Form>
    </Spin>
  );
}

export default connect(() => ({}))(
  Form.create({
    mapPropsToFields(props) {
      if (!isEmptyObject(props.orderFrom)) {
        return {
          orderFrom: Form.createFormField({ value: props.orderFrom }),
        };
      }
    },
  })(GuideInfoAdd)
);
